package com.leo.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.leo.TankApp;
import com.leo.common.GameType;
import com.leo.common.ItemType;
import com.leo.component.TankLevelComponent;
import com.leo.effects.FreezeEffect;
import com.leo.effects.HelmetEffect;
import com.leo.effects.ShipEffect;
import javafx.util.Duration;

/**
 * 敌人和道具的碰撞效果
 */
public class ItemEnemyHandler extends CollisionHandler {
    public ItemEnemyHandler() {
        super(GameType.ITEM, GameType.ENEMY);
    }

    @Override
    protected void onCollisionBegin(Entity a, Entity b) {
        // 获取道具类型
        ItemType itemType = a.getObject("itemType");
        FXGL.play("item.wav");
        a.removeFromWorld();

        switch (itemType) {
            case STAR -> b.getComponent(TankLevelComponent.class).upgrade();
            case GUN -> b.getComponent(TankLevelComponent.class).gradeFull();
            case TANK -> FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                // 玩家血量减少
                if (player.isActive()) {
                    HealthIntComponent healthIntComponent = player.getComponent(HealthIntComponent.class);
                    healthIntComponent.damage(1);
                    if (healthIntComponent.isZero()) {
                        b.removeFromWorld();
                        FXGL.spawn("explode", b.getCenter().subtract(50 / 2.0, 50 / 2.0));
                        FXGL.<TankApp>getAppCast().gameOver();
                    }
                }
            });
            case HEART -> FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                // 玩家血量减半
                if (player.isActive()) {
                    HealthIntComponent healthIntComponent = player.getComponent(HealthIntComponent.class);
                    healthIntComponent.damage(healthIntComponent.getValue() / 2);
                    if (healthIntComponent.isZero()) {
                        b.removeFromWorld();
                        FXGL.spawn("explode", b.getCenter().subtract(50 / 2.0, 50 / 2.0));
                        FXGL.<TankApp>getAppCast().gameOver();
                    }
                }
            });
            case BOMB -> FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                // 给玩家设置为一滴血
                if (player.isActive()) {
                    HealthIntComponent healthIntComponent = player.getComponent(HealthIntComponent.class);
                    healthIntComponent.setValue(1);
                }
            });
            case SHIP -> b.getComponent(EffectComponent.class).startEffect(new ShipEffect(Duration.INDEFINITE));
            case TIME -> FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER).forEach(player -> {
                // 玩家冻结效果
                if (player.isActive()) {
                    player.getComponent(EffectComponent.class).startEffect(new FreezeEffect(Duration.seconds(3)));
                }
            });
            case HELMET -> b.getComponent(EffectComponent.class).startEffect(new HelmetEffect(Duration.seconds(12)));
            case SPADE -> FXGL.<TankApp>getAppCast().unReinforce();
        }
    }
}
